// 业务相关的控制逻辑
import * as api from '../api'
import {parse as urlParse} from 'url'
import { parse as queryParse } from 'querystring'
import config from './../config'
export async function signature (ctx, next) {
  let url = ctx.query.url
  if(!url) ctx.thow(404)
  url = decodeURIComponent(url)
  const params = await api.getSignatureAsync(url)

  ctx.body = {
    success: true,
    params: params
  }
}
// 将路由指向 /oauth
export async function redirect (ctx, next) {
  const target = config.SITE_ROOT_URL + '/oauth'

  const scope = 'snsapi_userinfo'
  const {a, b} = ctx.query
  const params = `${a}_${b}`
  // 获取url
  const url = api.getAuthorizeURL(scope, target, params)

  ctx.redirect(url)
}

export async function oauth (ctx, next) {
  let url = ctx.query.url

  url = decodeURIComponent(url)
  const urlObj = urlParse(url)

  const params = queryParse(urlObj.query)
  const code = params.code
  const user = await api.getUserByCode(code)



  ctx.body = {
    success: true,
    data: user
  }

}